package com.ec.mn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ec.common.domain.Result;
import com.ec.mn.entity.HealthRecord;
import com.ec.mn.entity.WarningRules;
import com.ec.mn.service.WarningRulesService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "预警规则管理")
@RestController
@RequestMapping("/warningRules")
public class WarningRulesController {
    @Autowired
    private WarningRulesService warningRulesService;

    @ApiOperation("新增预警规则")
    @PostMapping("/add")
    public Result<WarningRules> add(@RequestBody WarningRules rule) {
        rule.setCreateTime(LocalDateTime.now());
     //   healthRecord.setRecordedAt(LocalDateTime.now());
        boolean success = warningRulesService.save(rule);
        return success ? Result.ok(rule) : Result.error("新增失败");
    }

    @ApiOperation("更新预警规则")
    @PostMapping("/update")
    public Result<WarningRules> update(@RequestBody WarningRules rule) {
        rule.setUpdateTime(LocalDateTime.now());
        boolean success = warningRulesService.updateById(rule);
        return success ? Result.ok(rule) : Result.error("更新失败");
    }

    @ApiOperation("删除预警规则")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        boolean success = warningRulesService.removeById(id);
        return success ? Result.ok() : Result.error("删除失败");
    }
    @ApiOperation("批量删除预警规则")
    @PostMapping("/batchDelete")
    public Result<Void> batchDelete(@RequestBody List<Long> ids) {
        boolean success = warningRulesService.removeByIds(ids);
        return success ? Result.ok() : Result.error("批量删除失败");
    }
    @ApiOperation("分页查询预警规则")
    @GetMapping("/page")
    public Result<Page<WarningRules>> getWarningRulePage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer current,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer size,
            @ApiParam("规则类型") @RequestParam(required = false) String recordType,
            @ApiParam("启用状态") @RequestParam(required = false) Integer status
    ) {
        Page<WarningRules> page = new Page<>(current, size);
        QueryWrapper<WarningRules> queryWrapper = new QueryWrapper<>();


        if (recordType != null && !recordType.trim().isEmpty()) {
            queryWrapper.eq("record_type", recordType);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }

        // 按创建时间倒序排列
        queryWrapper.orderByDesc("update_time");

        Page<WarningRules> result = warningRulesService.page(page, queryWrapper);
        return Result.ok(result);
    }


    @ApiOperation("根据ID查询预警规则")
    @GetMapping("/{id}")
    public Result<WarningRules> getById(@PathVariable Long id) {
        WarningRules rule = warningRulesService.getById(id);
        return rule != null ? Result.ok(rule) : Result.error("未找到");
    }

    @ApiOperation("查询所有预警规则")
    @GetMapping("/all")
    public Result<List<WarningRules>> getAll() {
        return Result.ok(warningRulesService.list());
    }

    @ApiOperation("查询所有启用的预警规则")
    @GetMapping("/active")
    public Result<List<WarningRules>> getAllActive() {
        return Result.ok(warningRulesService.getAllActiveRules());
    }

    @ApiOperation("根据健康数据类型查询启用的规则")
    @GetMapping("/active/{recordType}")
    public Result<WarningRules> getActiveByType(@PathVariable String recordType) {
        WarningRules rule = warningRulesService.getActiveRuleByRecordType(recordType);
        return rule != null ? Result.ok(rule) : Result.error("未找到");
    }
    //checkAndWarn
    @ApiOperation("预警")
    @PostMapping("/warning")
    public Result<String> checkAndWarn(@RequestBody HealthRecord healthRecord) {
        String rule = warningRulesService.checkAndWarn(healthRecord);
        return rule != null ? Result.ok(rule) : Result.error("预警失败");
    }
    @ApiOperation("查询所有数据类型")
    @GetMapping("/existingTypes")
    public Result<List<String>> getAllExistingTypes() {
        List<String> types = warningRulesService.getAllRecordTypes();
        return Result.ok(types);
    }

} 